package com.chuan.it_market.controller.buyer_controller;

import com.chuan.it_market.controller.BaseController;
import com.chuan.it_market.dao.GoodsDao;
import com.chuan.it_market.dao.SellerDao;
import com.chuan.it_market.pojo.dto.ResponseDTO;
import com.chuan.it_market.pojo.entity.Goods;
import com.chuan.it_market.pojo.entity.Seller;
import com.chuan.it_market.pojo.entity.ShopCar;
import com.chuan.it_market.pojo.vo.GoodsVO;
import com.chuan.it_market.pojo.vo.ShopCarVO;
import com.chuan.it_market.service.ShopCarService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller

@RequestMapping("/buyer/shopcar")
public class ShopcarController extends BaseController {
    @Resource
    ShopCarService shopCarService;
    @Resource
    SellerDao sellerDao;
    @Resource
    GoodsDao goodsDao;

    @RequestMapping("shopcarList")
    String showcarList(Model model){
        Integer bno=getUsreId();
        List<ShopCarVO> shopCarVOList= shopCarService.selectCarsByprimaryKey(bno);

        model.addAttribute("shopcar",shopCarVOList);
        return "/buyer/goods/shopcar-list";
    }

    @RequestMapping("delete/{cno}")
    @ResponseBody
    ResponseDTO delete(@PathVariable Integer cno){
        int i=shopCarService.delete(cno);
        if(i!=0){
            return ResponseDTO.ok("删除成功");
        }
        else return ResponseDTO.fail("删除失败");
    }

    @RequestMapping("insertShopcar")
    @ResponseBody
    ResponseDTO insertShopcar(ShopCar shopCar, HttpSession session){
        Integer bno=getUsreId();
        shopCar.setBno(bno);
        int i=shopCarService.insert(shopCar);
        if(i!=0){

            session.setAttribute("carCount",shopCarService.selectCarsCountByBno(getUsreId()));
            return ResponseDTO.ok("加入购物车成功");

        }
        else
        {
            return ResponseDTO.fail("加入购物车失败");
        }
    }
}
